/**
 * 
 */
package com.gr.staffpm.widget.dialog.feedback;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ResourceReference;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.rating.RatingPanel;
import org.apache.wicket.feedback.ContainerFeedbackMessageFilter;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.gr.staffpm.datatypes.User;
import com.gr.staffpm.model.UserModel;
import com.gr.staffpm.tasks.model.UserChoiceRenderer;
import com.gr.staffpm.widget.dialog.feedback.model.RatingModel;


/**
 * @author Graham Rhodes 11 Apr 2011 23:39:59
 */
public abstract class UserFeedbackPanel extends Panel {

    private static final long serialVersionUID = 1L;
    public static final ResourceReference STAR_EMPTY = new ResourceReference(AjaxFeedbackDialog.class, "StarEmpty.gif");
    public static final ResourceReference STAR_FULL = new ResourceReference(AjaxFeedbackDialog.class, "StarFull.gif");
    private static RatingModel rating = new RatingModel();
    private final DropDownChoice<User> teamMembers;
    private TextArea<String> commentBox;
    private RatingPanel ratingPanel;
    private final FeedbackPanel feedbackPanel;

    public UserFeedbackPanel(String id, List<User> team) {
        super(id);
        if (!team.isEmpty())
            add(teamMembers = new DropDownChoice<User>("teamMembers", new UserModel(team.get(0)), team, new UserChoiceRenderer()));
        else
            add(teamMembers = new DropDownChoice<User>("teamMembers", new UserModel(null), new ArrayList<User>(), new UserChoiceRenderer()));
        teamMembers.setRequired(true);

        ratingPanel = new RatingPanel("rating", new PropertyModel<Integer>(rating, "rating"), 5, true) {

            private static final long serialVersionUID = 1L;

            @Override
            public boolean onIsStarActive(int star) {
                return UserFeedbackPanel.rating.isActive(star);
            }

            @Override
            public void onRated(int rating, AjaxRequestTarget target) {
                UserFeedbackPanel.rating.setRating(rating);
            }

            @Override
            protected String getActiveStarUrl(int iteration) {
                return getRequestCycle().urlFor(STAR_FULL).toString();
            }

            @Override
            protected String getInactiveStarUrl(int iteration) {
                return getRequestCycle().urlFor(STAR_EMPTY).toString();
            }

        };
        ratingPanel.setRenderBodyOnly(false);
        ratingPanel.setOutputMarkupId(true);
        add(ratingPanel);

        Form<Void> form = new Form<Void>("form");

        feedbackPanel = new FeedbackPanel("feedback", new ContainerFeedbackMessageFilter(this));
        feedbackPanel.setOutputMarkupId(true);
        form.add(feedbackPanel);

        commentBox = new TextArea<String>("commentBox", new Model<String>());
        commentBox.setEscapeModelStrings(true);
        commentBox.setOutputMarkupId(true);
        commentBox.setRequired(true);
        form.add(commentBox);

        form.add(new AjaxButton("submitButton") {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                rating.setComment(commentBox.getValue());
                UserFeedbackPanel.this.onSubmit(target, rating, teamMembers.getModelObject());
            }

            @Override
            protected void onError(AjaxRequestTarget target, Form<?> form) {
                target.addComponent(feedbackPanel);
            };

        });

        form.add(new AjaxButton("cancelButton") {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                UserFeedbackPanel.this.onCancel(target);
            }

        });
        add(form);
    }

    public abstract void onSubmit(AjaxRequestTarget target, RatingModel rating, User user);

    public abstract void onCancel(AjaxRequestTarget target);

}
